package com.group2.operations.mapper;

import com.group2.operations.model.Client;
import com.group2.operations.model.dto.CountPeopleDTO;
import com.group2.operations.model.vo.ClientInfoVO;
import org.apache.ibatis.annotations.*;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;


import java.util.Date;
import java.util.List;

@Repository
@Mapper
public interface ClientMapper {
    /**
     * 根据厅店ID查询客户
     * @param sid 厅店ID
     * @return
     */
    @Select("select * from client where sid=#{sid} order by time desc")
    List<Client> queryClientsBySid(int sid);

    /**
     * 插入客户数据
     * @param sid 厅店ID
     * @param info 客户信息
     * @param time 入户时间
     * @throws DataAccessException
     */
    @Insert("insert into client(`id`, `name`, `number`, `phone`, `time`, `sid`) values(null, #{info.name}, #{info.number}, #{info.phone}, #{time}, #{sid})")
    void addClient(@Param("sid") int sid, @Param("info") ClientInfoVO info, @Param("time") Date time) throws DataAccessException;

    /**
     * 更新客户数据
     * @param cid 客户ID
     * @param info 客户信息
     * @throws DataAccessException
     */
    @Update("update client set name=#{info.name}, number=#{info.number}, phone=#{info.phone} where id=#{cid}")
    void updateClient(@Param("cid") int cid, @Param("info") ClientInfoVO info) throws DataAccessException;

    /**
     * 删除客户信息
     * @param cid 客户ID
     */
    @Delete("delete from client where id=#{cid}")
    void deleteClient(int cid) throws DataAccessException;

    /**
     * 查询厅店每天的到店人数
     * @param sid
     * @return
     */
    @Select("select count(*) as count, DATE_FORMAT(time, '%Y-%m-%d') as time " +
            " from client " +
            " where sid=#{sid} " +
            " group by DATE_FORMAT(time, '%Y-%m-%d')")
     List<CountPeopleDTO> countByDay(int sid);
}
